<template>
	<el-container>
		<el-aside width="220px" v-loading="showGrouploading">
			<el-tree ref="category"
					 class="menu"
					 node-key="label"
					 :data="category"
					 :props="groupsProps"
					 @node-click="updateLog"
					 :highlight-current="true"
					 :expand-on-click-node="false">
			</el-tree>
		</el-aside>
		<el-container>
			<el-main class="nopadding">
				<el-container>
					<el-header style="height: auto;">
						<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
					</el-header>
					<el-header>
						<div class="left-panel">
							<el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length==0" @click="batch_del"></el-button>
							<el-button type="warning" icon="el-icon-delete" @click="deleteSysLog(1)">一周前</el-button>
							<el-button type="warning" icon="el-icon-delete" @click="deleteSysLog(2)">半月前</el-button>
							<el-button type="warning" icon="el-icon-delete" @click="deleteSysLog(3)">一月前</el-button>
						</div>
						<div class="right-panel">
							<el-form-item label="关联人" style="padding-top: 20px;" label-width="80px">
								<select-user-dialog v-model:accountId="search.accountId" :mode="false"/>
							</el-form-item>
							<div class="right-panel-search">
								<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
							</div>
						</div>
					</el-header>
					<el-main class="nopadding">
						<scTable ref="table" :apiObj="apiObj" row-key="logId" stripe highlightCurrentRow @selection-change="selectionChange">
							<el-table-column type="selection" width="50"></el-table-column>
							<el-table-column label="#" type="index" width="50"></el-table-column>
							<el-table-column label="事件类型" prop="eventName" width="150"></el-table-column>
							<el-table-column label="客户端IP" prop="ip" width="150"></el-table-column>
							<el-table-column label="相关人员" prop="createUserName" width="150"></el-table-column>
							<el-table-column label="日志时间" prop="createTime" width="170"></el-table-column>
							<el-table-column label="日志摘要" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
							<el-table-column label="操作" fixed="right" align="right" width="100">
								<template #default="scope">
									<el-button-group>
										<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
											<template #reference>
												<el-button text type="primary" size="small">删除</el-button>
											</template>
										</el-popconfirm>
									</el-button-group>
								</template>
							</el-table-column>
						</scTable>
					</el-main>
				</el-container>
			</el-main>
		</el-container>
	</el-container>

</template>

<script>
import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog.vue";
export default {
	name: 'Operation Log',
	components: {
		selectUserDialog,
		scSelectFilter
	},
	data() {
		return {
			showGrouploading: false,
			selection:[],
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				}
			],
			category: [],
			groupsProps: {
				label:"eventName",
				value: "eventType"
			},
			apiObj: this.$API.system.sysLog.getAllSysLogList,
			search: {
				date:'',
				accountId:'',
				keyword: "",
				eventType:""
			}
		}
	},
	mounted() {
		this.getSysEventTree();
	},
	methods: {
		//加载树数据
		async getSysEventTree(){
			this.showGrouploading = true;
			var res = await this.$API.system.sysEvent.getSysEventTree.get();
			this.showGrouploading = false;
			this.category = res.data;
		},
		filterChange(data){
			this.search.date = data.date;
			this.$refs.table.upData(this.search)
		},
		updateLog(item)
		{
			if(item.parentId!='0')
			{
				this.search.eventType = item.eventType;
				this.$refs.table.upData(this.search)
			}
		},
		//删除
		async table_del(row) {
			var reqData = {logId: row.logId}
			var res = await this.$API.system.sysLog.deleteSysLog.post(reqData);
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async deleteSysLog(flag) {
			let msg = "";
			if(flag=='1')
			{
				msg ="您确定删除一周前的所有日志吗?"
			}else if(flag=='2')
			{
				msg ="您确定删除半月前的所有日志吗?"
			} else if(flag=='3')
			{
				msg ="您确定删除一月前的所有日志吗?"
			}
			await this.$confirm(msg, '提示', {
				type: 'warning'
			}).then(async() => {
			var res = await this.$API.system.sysLog.deleteSysLogByTime.post({flag:flag});
			if (res.code == 200) {
				this.$refs.table.refresh()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
			}).catch(() => {

			})
		},
		async upsearch() {
			this.$refs.table.upData(this.search)
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		async batch_del() {
			await this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(async() => {
				var logIdArr = [];
				for (var i = 0; i < this.selection.length; i++) {
					logIdArr.push(this.selection[i].logId);
				}
				const loading = this.$loading();
				var res = await this.$API.system.sysLog.deleteSysLogByIds.post({ids: logIdArr});
				if (res.code == 200) {
					this.$refs.table.refresh()
					this.$message.success(res.message)
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				loading.close();
			}).catch(() => {

			})
		},
	}
}
</script>

<style>
</style>
